<?php
namespace app\shouyin\admin;
use app\system\admin\Admin;
use app\shouyin\model\ProductOrder as ProductOrderModel;
use app\system\model\systemUser as systemUserModel;
use app\finance\model\SystemBalanceLog as SystemBalanceLogModel;
use app\user\model\User as UserModel;
use app\product\model\Product as ProductModel;
use app\finance\model\Account as AccountModel;
use app\warehouse\model\Warehouse as WarehouseModel;
use app\shouyin\model\UserCardLog as UserCardLogModel;
use app\shouyin\model\UserBalanceLog as UserBalanceLogModel;
use think\Db;
use app\warehouse\model\Stock as StockModel;
class Product extends Admin
{
    protected $hisiModel = 'ProductOrder';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名


    public function index()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $where['shop_id'] = session('shop_id');
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);

            $user_id    = $this->request->param('user_id', '');
            $start    = $this->request->param('start', '2000-01-01');
            $end    = $this->request->param('end', date('Y-m-d H:i:s',time()));
            if(!empty($user_id)){
                $where['user_id'] = $user_id;
            }
            if(empty($start)){
                $start = '2000-01-01';
            }
            if(empty($end)){
                $end = date('Y-m-d',time());
            }


            $data['data'] = ProductOrderModel::with(['hasShop','hasUser'])
                            ->where($where)
                            ->where('createtime','between',[strtotime($start),strtotime($end)])
                            ->field('*,cashier_id as cashier_text, finance_id as finance_text')
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
                            // halt($data['data']->cashier_text);
            $data['count'] = ProductOrderModel::where($where)->where('createtime','between',[strtotime($start),strtotime($end)])->count('id');
            $data['code'] = 0;
            return json($data);
        }
        $users = UserModel::field('id,nick')->all();
        $this->assign('users',$users);
        return $this->fetch();
    }
    public function edit()
    {
        $id = $this->request->param('id');
        $formData = ProductOrderModel::get($id);
        $formData->user_name = UserModel::where('id',$formData['user_id'])->value('nick');
        $formData->cashier_name = systemUserModel::where('id',$formData['cashier_id'])->value('nick');
        $this->assign('formData',$formData);
        $this->assign('financeList', systemUserModel::where('shop_id',session('shop_id'))->where('type',6)->all());

        $productList = ProductModel::all();
        $this->assign('productList',$productList);

        // 用户
        $userList = UserModel::field('id,nick')->all();
        $this->assign('userList',$userList);

        // 收银
        $cashiers = systemUserModel::where('shop_id',session('shop_id'))->where('type',3)->all();
        $this->assign('cashiers',$cashiers);

        // 顾问
        $advisers = systemUserModel::where('shop_id',session('shop_id'))->where('type',5)->all();
        $this->assign('advisers',$advisers);

        // 美容师
        $technicians = systemUserModel::where('shop_id',session('shop_id'))->where('type',7)->all();
        $this->assign('technicians',$technicians);

        // 账户
        $accounts = AccountModel::where('status',1)->all();
        // $accounts[] = ['id' => -1, 'name' => "产品卡"];
        $accounts[] = ['id' => -2, 'name' => "赠送"];
        $accounts[] = ['id' => -5, 'name' => "会员卡赠送"];
        $this->assign('accounts',$accounts);
        // 仓库
        $warehouses = WarehouseModel::where('shop_id',session('shop_id'))->all();
        $this->assign('warehouses',$warehouses);

        if(!is_empty($this->request->param('xx')) && $this->request->param('xx') == 2){
            $this->view->engine->layout(false);
            return $this->fetch('dayin');
        }
        return $this->fetch();
    }
    public function editDo()
    {
        $data = $this->request->post();
        if($data['status'] != 1){
            if(empty($data['finance_id'])){
                return $this->axiosMsg('审核财务为必填项！', false);
            }
            if(empty($data['status'])){
                return $this->axiosMsg('审核意见为必填项！', false);
            }
        }

        $ProductOrderModel = new ProductOrderModel;
        try {
            $ProductOrderModel->save($data,['id'=>$data['id']]);
        } catch (\Exception $e) {
            return $this->axiosMsg('提交出错，请联系管理员！'.$e->getMessage(), false);
        }

        return $this->axiosMsg('审核完成！',true);
    }
    public function add()
    {
        $this->assign('sn',create_order_sn());
               // 产品
        $productList = ProductModel::all();
        $this->assign('productList',$productList);

        // 用户
        $userList = UserModel::field('id,nick')->all();
        $this->assign('userList',$userList);

        // 收银
        $cashiers = systemUserModel::where('shop_id',session('shop_id'))->where('type',3)->all();
        $this->assign('cashiers',$cashiers);

        // 顾问
        $advisers = systemUserModel::where('shop_id',session('shop_id'))->where('type',5)->all();
        $this->assign('advisers',$advisers);

        // 美容师
        $technicians = systemUserModel::where('shop_id',session('shop_id'))->where('type',7)->all();
        $this->assign('technicians',$technicians);

        // 账户
        $accounts = AccountModel::where('status',1)->all();
        // $accounts[] = ['id' => -1, 'name' => "产品卡"];
        $accounts[] = ['id' => -2, 'name' => "赠送"];
        $accounts[] = ['id' => -5, 'name' => "会员卡赠送"];
        $this->assign('accounts',$accounts);

        // 仓库
        $warehouses = WarehouseModel::where('shop_id',session('shop_id'))->all();
        $this->assign('warehouses',$warehouses);
        return $this->fetch('add');
    }
    public function addDo()
    {
        $data = $this->request->post();
        // halt($data);
        $data['shop_id'] = session('shop_id');
        if(empty($data['user_id'])){
            return $this->axiosMsg('会员为必选项！', false);
        }
        if(empty($data['cashier_id'])){
            return $this->axiosMsg('收银为必选项！', false);
        }
        // foreach($data['productData'] as $k=>$v){
        //     if(!isset($v['warehouse_id']) || empty($v['warehouse_id']))
        //     {
        //         return $this->axiosMsg('仓库为必填项');
        //     }
        // }
        if(empty($data['goods'])){
            return $this->axiosMsg('请选择产品！', false);
        }
        $ProductOrderModel = new ProductOrderModel;
        try {
            $ProductOrderModel->create($data);
        } catch (\Exception $e) {
            return $this->axiosMsg('提交出错，请联系管理员！'.$e->getMessage(), false);
        }

        return $this->axiosMsg('提交完成！',true);
    }
    public function lookDetil()
    {
        $id = $this->request->param('id');
        $formData = ProductOrderModel::get($id);
        $formData->user_name = UserModel::where('id',$formData['user_id'])->value('nick');
        $formData->cashier_name = systemUserModel::where('id',$formData['cashier_id'])->value('nick');
        $this->assign('formData',$formData);
        $this->assign('financeList', systemUserModel::where('shop_id',session('shop_id'))->where('type',6)->all());

        $productList = ProductModel::all();
        $this->assign('productList',$productList);

        // 用户
        $userList = UserModel::field('id,nick')->all();
        $this->assign('userList',$userList);

        // 收银
        $cashiers = systemUserModel::where('shop_id',session('shop_id'))->where('type',3)->all();
        $this->assign('cashiers',$cashiers);

        // 顾问
        $advisers = systemUserModel::where('shop_id',session('shop_id'))->where('type',5)->all();
        $this->assign('advisers',$advisers);

        // 美容师
        $technicians = systemUserModel::where('shop_id',session('shop_id'))->where('type',7)->all();
        $this->assign('technicians',$technicians);

        // 账户
        $accounts = AccountModel::where('status',1)->all();
        // $accounts[] = ['id' => -1, 'name' => "产品卡"];
        $accounts[] = ['id' => -2, 'name' => "赠送"];
        $accounts[] = ['id' => -5, 'name' => "会员卡赠送"];
        $this->assign('accounts',$accounts);
        // 仓库
        $warehouses = WarehouseModel::where('shop_id',session('shop_id'))->all();
        $this->assign('warehouses',$warehouses);
        return $this->fetch();
    }


    public function kgkd()
    {
        $id = $this->request->param('id');
        $formData = ProductOrderModel::get($id);
        $formData->user_name = UserModel::where('id',$formData['user_id'])->value('nick');
        $formData->cashier_name = systemUserModel::where('id',$formData['cashier_id'])->value('nick');
        $this->assign('formData',$formData);
        $this->assign('financeList', systemUserModel::where('shop_id',session('shop_id'))->where('type',6)->all());

        $productList = ProductModel::all();
        $this->assign('productList',$productList);

        // 用户
        $userList = UserModel::field('id,nick')->all();
        $this->assign('userList',$userList);

        // 收银
        $cashiers = systemUserModel::where('shop_id',session('shop_id'))->where('type',3)->all();
        $this->assign('cashiers',$cashiers);

        // 顾问
        $advisers = systemUserModel::where('shop_id',session('shop_id'))->where('type',5)->all();
        $this->assign('advisers',$advisers);

        // 美容师
        $technicians = systemUserModel::where('shop_id',session('shop_id'))->where('type',7)->all();
        $this->assign('technicians',$technicians);

        $ware_admins = systemUserModel::where('shop_id',session('shop_id'))->where('type',4)->all();
        $this->assign('ware_admins',$ware_admins);
        // 账户
        $accounts = AccountModel::where('status',1)->all();
        // $accounts[] = ['id' => -1, 'name' => "产品卡"];
        $accounts[] = ['id' => -2, 'name' => "赠送"];
        $accounts[] = ['id' => -5, 'name' => "会员卡赠送"];
        $this->assign('accounts',$accounts);
        // 仓库
        // 7-1 修改商品库为  所有仓库
        // $warehouses = WarehouseModel::where('shop_id',session('shop_id'))->all();
        $warehouses = WarehouseModel::where('shop_id',session('shop_id'))->all();
        $this->assign('warehouses',$warehouses);

        if(!is_empty($this->request->param('xx')) && $this->request->param('xx') == 2){
            $this->view->engine->layout(false);
            return $this->fetch('dayin');
        }
        return $this->fetch();
    }
    public function kgkdDo()
    {
        $data = $this->request->post();
        if(empty($data['ware_admin'])){
            return $this->axiosMsg('审核库管为必填项！', false);
        }
        // $data['status'] = 4;
        if($data['status'] == 4){
            foreach($data['productData'] as $k=>$v){
                if(!isset($v['warehouse_id']) || empty($v['warehouse_id']))
                {
                    return $this->axiosMsg('仓库为必填项');
                }
            }

        }
        $ProductOrderModel = new ProductOrderModel;
            $data['goods'] = $data['productData'];

        try {
            $ProductOrderModel->save($data,['id'=>$data['id']]);
        } catch (\Exception $e) {
            return $this->axiosMsg('提交出错，请联系管理员！'.$e->getMessage(), false);
        }

        return $this->axiosMsg('审核完成！',true);
    }
    // 退回收银修改
    public function sy()
    {
        $id = $this->request->param('id');
        $formData = ProductOrderModel::get($id);
        $formData->user_name = UserModel::where('id',$formData['user_id'])->value('nick');
        $userModel = new UserModel();

        $balance = $userModel->getUserbalance($formData['user_id'],1);
        $balance2 = $userModel->getUserbalance($formData['user_id'],2);
        $this->assign('balance',$balance);
        $this->assign('balance2',$balance2);
        // halt($balance2);
        $formData->cashier_name = systemUserModel::where('id',$formData['cashier_id'])->value('nick');
        $this->assign('formData',$formData);
        $this->assign('financeList', systemUserModel::where('shop_id',session('shop_id'))->where('type',6)->all());

        $productList = ProductModel::all();
        $this->assign('productList',$productList);

        // 用户
        $userList = UserModel::field('id,nick')->all();
        $this->assign('userList',$userList);

        // 收银
        $cashiers = systemUserModel::where('shop_id',session('shop_id'))->where('type',3)->all();
        $this->assign('cashiers',$cashiers);

        // 顾问
        $advisers = systemUserModel::where('shop_id',session('shop_id'))->where('type',5)->all();
        $this->assign('advisers',$advisers);

        // 美容师
        $technicians = systemUserModel::where('shop_id',session('shop_id'))->where('type',7)->all();
        $this->assign('technicians',$technicians);

        $ware_admins = systemUserModel::where('shop_id',session('shop_id'))->where('type',4)->all();
        $this->assign('ware_admins',$ware_admins);
        // 账户
        $accounts = AccountModel::where('status',1)->all();
        // $accounts[] = ['id' => -1, 'name' => "产品卡"];
        $accounts[] = ['id' => -2, 'name' => "赠送"];
        $accounts[] = ['id' => -5, 'name' => "会员卡赠送"];
        $this->assign('accounts',$accounts);
        // 仓库
        $warehouses = WarehouseModel::where('shop_id',session('shop_id'))->all();
        $this->assign('warehouses',$warehouses);

        if(!is_empty($this->request->param('xx')) && $this->request->param('xx') == 2){
            $this->view->engine->layout(false);
            return $this->fetch('dayin');
        }
        return $this->fetch();
    }
    public function sykd()
    {
        $id = $this->request->param('id');
        $formData = ProductOrderModel::get($id);
        $formData->user_name = UserModel::where('id',$formData['user_id'])->value('nick');
        $userModel = new UserModel();

        $balance = $userModel->getUserbalance($formData['user_id'],1);
        $balance2 = $userModel->getUserbalance($formData['user_id'],2);
        $this->assign('balance',$balance);
        $this->assign('balance2',$balance2);
        // halt($balance2);
        $formData->cashier_name = systemUserModel::where('id',$formData['cashier_id'])->value('nick');
        $this->assign('formData',$formData);
        $this->assign('financeList', systemUserModel::where('shop_id',session('shop_id'))->where('type',6)->all());

        $productList = ProductModel::all();
        $this->assign('productList',$productList);

        // 用户
        $userList = UserModel::field('id,nick')->all();
        $this->assign('userList',$userList);

        // 收银
        $cashiers = systemUserModel::where('shop_id',session('shop_id'))->where('type',3)->all();
        $this->assign('cashiers',$cashiers);

        // 顾问
        $advisers = systemUserModel::where('shop_id',session('shop_id'))->where('type',5)->all();
        $this->assign('advisers',$advisers);

        // 美容师
        $technicians = systemUserModel::where('shop_id',session('shop_id'))->where('type',7)->all();
        $this->assign('technicians',$technicians);

        $ware_admins = systemUserModel::where('shop_id',session('shop_id'))->where('type',4)->all();
        $this->assign('ware_admins',$ware_admins);
        // 账户
        $accounts = AccountModel::where('status',1)->all();
        // $accounts[] = ['id' => -1, 'name' => "产品卡"];
        $accounts[] = ['id' => -2, 'name' => "赠送"];
        $accounts[] = ['id' => -5, 'name' => "会员卡赠送"];
        $this->assign('accounts',$accounts);
        // 仓库
        $warehouses = WarehouseModel::where('shop_id',session('shop_id'))->all();
        $this->assign('warehouses',$warehouses);

        if(!is_empty($this->request->param('xx')) && $this->request->param('xx') == 2){
            $this->view->engine->layout(false);
            return $this->fetch('dayin');
        }
        return $this->fetch();
    }
    public function sykdDo()
    {
        $postData = $this->request->post();
        // halt($postData);
        if(empty($postData['user_id'])){
            return $this->axiosMsg('会员为必选项！', false);
        }
        $ProductOrderModel = new ProductOrderModel;
        if($postData['status'] == 3){
            if($ProductOrderModel->save($postData,['id'=>$postData['id']]))
            {
                return $this->axiosMsg('审核完成',true);
            }
            return $this->axiosMsg('发生错误，请联系技术人员排查',false);
        }
        $userModel = new UserModel;
        $UserCardLogModel = new UserCardLogModel;
        // 检查用户余额是否足够
        $userBalance = $userModel->getUserBalance($postData['user_id'],1);
        $userBalanceZeng = $userModel->getUserBalance($postData['user_id'],2);
        $price1 = 0; // 余额支付
        $price2 = 0; // 非现金支付
        $price5 = 0; // 会员卡赠送余额
        $cards = [];

        if(empty($postData['goods'])){
            return $this->axiosMsg('请选择产品！', false);
        }


        $SystemBalanceLogModel = new SystemBalanceLogModel;
        $StockModel = new StockModel;
        $postData['shop_id'] = session('shop_id');
        // $warehouse_id = Db::name('warehouse')->where('shop_id',session('shop_id'))->value('id');
        // 启动事务
        Db::startTrans();
        try {
            foreach ($postData['goods'] as $key => $value) {
                if($value['account_id'] == 0){   // 余额消费
                    $price1 += $value['price2'];
                }else if($value['account_id'] == -1){ // 产品卡
                    $cards[$value['product_id']][] = $value['num'];
                }else if($value['account_id'] == -5){ // 会员卡赠送余额
                    $price5 += $value['price2'];
                }else{  // 现金消费

                }
            }
            foreach ($cards as $key => $value) {
                $project_num = Db::name('user_card_log')->where('user_id',$postData['user_id'])->where('pid',$key)->where('type',2)->where('use',1)->count('id');
                // 检查用户会员卡是否足够
                if(array_sum($cards[$key]) > $project_num  || $project_num == 0){
                    throw new \Exception("用户产品卡不足！");
                    // return $this->axiosMsg('用户会员卡不足！', false);
                }
            }
            // 检查用户余额是否足够
            // 2020-07-24 去掉限制
            // if($price1 > $userBalance){
            //     throw new \Exception("用户会员卡不足！");
            // }
            if($price5 > $userBalanceZeng){
                throw new \Exception("用户会员卡赠送余额不足！");
            }
            $stock = [];
            $deposit = [];
            // 扣除余额，现金，会员卡

            $UserBalanceLogModel = new UserBalanceLogModel;
            // halt($postData['goods']);
            foreach ($postData['goods'] as $key => $value) {
                if($value['account_id'] == 0){   // 会员卡消费
                    $ins1 = [
                        'user_id' => $postData['user_id'],
                        'type' => 3, // 购买产品使用
                        'price' => $value['price2'],
                        'sn' => $postData['sn'],
                        'is_give' => 1,
                        'createtime' => time()
                    ];
                    $UserBalanceLogModel->insert($ins1);
                    $account_type = '会员卡';
                }else if($value['account_id'] == -1){ // 产品卡
                    $product_ids = UserCardLogModel::where('user_id', $postData['user_id'])->where('pid',$value['product_id'])->where('use',1)->where('type',2)->limit($value['num'])->column('id');

                    $UserCardLogModel->where('id','in',$product_ids)->update(['use'=>2,'use_sn'=>$postData['sn']]);
                    $account_type = '产品卡';
                }else if($value['account_id'] == -2){ // 赠送
                    $account_type = '赠送';

                }else if($value['account_id'] == -5){ // 会员卡赠送
                    $ins5 = [
                        'user_id' => $postData['user_id'],
                        'type' => 3, // 购买产品使用
                        'price' => $value['price2'],
                        'sn' => $postData['sn'],
                        'is_give' => 2,
                        'createtime' => time()
                    ];
                    $UserBalanceLogModel->insert($ins5);

                    $account_type = '会员卡赠送余额';

                }else{  // 现金消费
                    $ins2 = [
                        'user_id' => $postData['user_id'],
                        'type' => 2, // 购买产品使用
                        'price' => $value['price2'],
                        'sn' => $postData['sn'],
                        'account_id' => $value['account_id'],
                        'shop_id' => $postData['shop_id'],
                        'createtime' => time(),
                        'updatetime' => time(),
                    ];
                    $SystemBalanceLogModel->insert($ins2);
                    $account_type = '现金';
                }


                // 绩效********************************************************
                $meirongshi = Db::name('system_user')->where('id','in',$value['technician_ids'])->column('id,nick');
                $jixiao = [];
                // dump($value );
                $jixiao[] = [
                    'admin_id' => $value['adviser_id'],
                    'user_id' => $postData['user_id'],
                    'project' => '产品购买--'.$value['name'],
                    'num' => $value['num'],
                    'price' => $value['price2'],
                    'shougong' => 0,
                    'settlement' => $account_type,
                    'shop_id' => $postData['shop_id'],
                    'admin_type' => '顾问',
                    'proportion' => 0,
                    'createtime' => date('Y-m-d H:i:s',time()),
                    'sn' => $postData['sn'],
                ];
                foreach ($meirongshi as $k => $v) {
                    $jixiao[] = [
                        'admin_id' => $k,
                        'user_id' => $postData['user_id'],
                        'project' => '产品购买--'.$value['name'],
                        'num' => $value['num'],
                        // 'price' => $value['price2'],
                        'price' => round(($value['price2'] / count($meirongshi)),2),
                        'shougong' => 0,
                        'settlement' => $account_type,
                        'shop_id' => $postData['shop_id'],
                        'admin_type' => '美容师',
                        'proportion' => 0,
                        'createtime' => date('Y-m-d H:i:s',time()),
                        'sn' => $postData['sn'],
                    ];
                }
                if(count($jixiao) > 0){
                    Db::name('achievements')->insertAll($jixiao);
                }
                // 绩效********************************************************
                // -----------------------------扣库存-start-------------------------------------
                $begin_num = $StockModel->getBeginNum($value['warehouse_id'],$value['product_id']);
                // 成本价
                $product_price = $StockModel->getProductPrice($value['warehouse_id'],$value['product_id']);
                $product_info = ProductModel::get($value['product_id']);
                $warehouseModel = new WarehouseModel;
                if($product_price <= 0){  // 仓库没有库存记录的情况下会出现成本价为0的情况

                    $warehouse_type = $warehouseModel->warehouseType($value['warehouse_id']);
                    if($warehouse_type == 1){  //  配料库
                        $product_price = $product_info->price1 / $product_info->specs;
                    }else{   //  总库
                        $product_price = $product_info->price1;
                    }
                }
                // 是否寄存
                if($value['is_deposit'] == 2){


                    $deposit[] = [
                        'user_id' => $postData['user_id'],
                        'product_id' => $value['product_id'],
                        'num' => $value['num'],
                        'address' => '产品单寄存',
                        'status' => 1,
                        'createtime' => time(),
                        'updatetime' => time(),
                        'bak' => '产品单寄存',
                        'shop_id' => session('shop_id'),
                        'sn' => $postData['sn'],
                        'price' => $value['price2'],
                        'warehouse_id' => $value['warehouse_id'],
                        'unit_price' => $product_price * $value['num']
                    ];
                }else{

                    $stock[] = [
                        'product_id' => $value['product_id'],
                        'product_num' => $value['num'],
                        'unit_price' => $product_price * $value['num'],
                        'price' => $value['price2'],
                        'product_name' => $value['name'],
                        'shop_id' => session('shop_id'),
                        'sn' => $postData['sn'],
                        'type' => '出库',
                        'detailed_type' => 2,
                        'user_id' => $postData['user_id'],
                        'warehouse_id' => $value['warehouse_id'],
                        'status' => 1,
                        'bak' => '产品单出库',
                        'createtime' => time(),
                        'begin_num'     => $begin_num,
                        'end_num'      => $begin_num - $value['num']
                    ];
                }

                // -----------------------------扣库存-end-------------------------------------

            }

            // 更新库存
            // $StockModel = new StockModel;
            if(!empty($stock)){
                $StockModel->insertAll($stock);
            }

            if(!empty($deposit)){
                 $aa = Db::name('deposit')->insertAll($deposit);
            }
            // dump($postData);
            // $StockModel->saveAll($stock);
            // 生成订单
            $ProductOrderModel->save($postData,['id'=>$postData['id']]);

            Db::name('user')->where('id',$postData['user_id'])->update(['last_login_time'=>time()]);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->axiosMsg($e->getMessage(), false);
        }


        return $this->axiosMsg('审核完成！',true);
    }
    public function cwsh()
    {
        $id = $this->request->param('id');
        $formData = ProductOrderModel::get($id);
        $formData->user_name = UserModel::where('id',$formData['user_id'])->value('nick');
        $formData->cashier_name = systemUserModel::where('id',$formData['cashier_id'])->value('nick');
        $this->assign('formData',$formData);
        $this->assign('financeList', systemUserModel::where('shop_id',session('shop_id'))->where('type',6)->all());

        $productList = ProductModel::all();
        $this->assign('productList',$productList);

        // 用户
        $userList = UserModel::field('id,nick')->all();
        $this->assign('userList',$userList);

        // 收银
        $cashiers = systemUserModel::where('shop_id',session('shop_id'))->where('type',3)->all();
        $this->assign('cashiers',$cashiers);

        // 顾问
        $advisers = systemUserModel::where('shop_id',session('shop_id'))->where('type',5)->all();
        $this->assign('advisers',$advisers);

        // 美容师
        $technicians = systemUserModel::where('shop_id',session('shop_id'))->where('type',7)->all();
        $this->assign('technicians',$technicians);

        $ware_admins = systemUserModel::where('shop_id',session('shop_id'))->where('type',4)->all();
        $this->assign('ware_admins',$ware_admins);
        // 账户
        $accounts = AccountModel::where('status',1)->all();
        // $accounts[] = ['id' => -1, 'name' => "产品卡"];
        $accounts[] = ['id' => -2, 'name' => "赠送"];
        $accounts[] = ['id' => -5, 'name' => "会员卡赠送"];
        $this->assign('accounts',$accounts);
        // 仓库
        $warehouses = WarehouseModel::where('shop_id',session('shop_id'))->all();
        $this->assign('warehouses',$warehouses);

        return $this->fetch();
    }
    public function cwshDo()
    {
        $data = $this->request->post();

        // $data['status'] = 4;
        if(empty($data['status']) || !isset($data['status']) || $data['status'] == 0){
            return $this->axiosMsg('审核意见必填',false);
        }
        $ProductOrderModel = new ProductOrderModel;

        try {
            $ProductOrderModel->save($data,['id'=>$data['id']]);
        } catch (\Exception $e) {
            return $this->axiosMsg('提交出错，请联系管理员！'.$e->getMessage(), false);
        }
        return $this->axiosMsg('审核完成', true);
    }


    /**
     * 删除产品单
     *
     * @return void
     */
    public function del()
    {
        $id = $this->request->param('id');
        $model = new ProductOrderModel;
        $proInfo = $model->get($id);
        Db::startTrans();
        try {
            // 删除有多种结果  1 现金支付的删除银行流水  2 会员卡支付的删除会员卡流水记录   3 疗程卡的还需修改使用记录  删除流水记录

            // 库存记录
            Db::name('stock')->where('sn',$proInfo['sn'])->delete();
            // 寄存单
            Db::name('deposit')->where('sn',$proInfo['sn'])->delete();
            // 1 现金支付的删除银行流水
            Db::name('system_balance_log')->where('sn',$proInfo['sn'])->delete();

            // 2 会员卡支付的删除会员卡流水记录
            Db::name('user_balance_log')->where('sn',$proInfo['sn'])->delete();
            // 绩效
            Db::name('achievements')->where('sn',$proInfo['sn'])->delete();
            // 产品单
            Db::name('product_order')->where('id',$id)->delete();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return $this->axiosMsg($e->getMessage(),false);
        }
        return $this->axiosMsg('删除成功！',true);
    }
}
